//
//  CouponConfig.swift
//  CMB250716
//
//  Created by Assistant on 2025-01-02.
//

import Foundation

struct CouponConfig {
    
    // MARK: - 解锁条件类型枚举
    enum UnlockConditionType: Equatable {
        case loginRequired
        case membershipLevel(String)
        case memberInfoComplete
        case birthdayMonthRequired
        case activationConditions(String)
        case timeSlotRestricted
        case usageLimitReached
        case usageFrequencyReached
        case expired
        case inactive
        case none // 无限制，可直接使用
    }
    
    // MARK: - 解锁条件配置
    struct UnlockConditions {
        /// 根据优惠券代码获取解锁条件类型
        static func getUnlockConditionType(for couponCode: String, user: User?, dataManager: DataManager) -> UnlockConditionType {
            guard let couponData = coupons.first(where: { $0.code == couponCode }) else {
                return .inactive
            }
            
            // 检查优惠券是否激活
            if !couponData.isActive {
                return .inactive
            }
            
            // 检查是否需要登录
            if couponData.requiresLogin && user == nil {
                return .loginRequired
            }
            
            // 基础新手礼券：用户登录后自动解锁
            let basicCoupons = ["NEWMEMBER5", "WEEKEND50", "HIGHBALL", "CALFCOFFEE", "MYSTERYGIFT"]
            if basicCoupons.contains(couponCode) && user != nil {
                return .none
            }
            
            // 检查会员等级要求
            if let requiredLevel = couponData.requiresMembershipLevel,
               let currentUser = user {
                if currentUser.membershipLevel.rawValue != requiredLevel {
                    return .membershipLevel(requiredLevel)
                }
            }
            
            // 检查特殊优惠券的特定条件
            switch couponCode {
            case "BIRTHDAY30":
                // 生日月份专属福利需要在生日月份且完善个人信息
                if let currentUser = user {
                    // 首先检查是否完善了个人信息（包括生日信息）
                    if !currentUser.isMemberInfoComplete {
                        return .memberInfoComplete
                    }
                    // 检查是否在生日月份
                    let currentMonth = Calendar.current.component(.month, from: Date())
                    if let birthMonth = Int(currentUser.birthMonth ?? ""), birthMonth != currentMonth {
                        return .birthdayMonthRequired
                    }
                } else {
                    return .loginRequired
                }
            case "MEMBER_INFO_COMPLETE":
                // 检查会员信息是否完善 - 必须完善所有4项信息：姓名、手机号、邮箱、生日
                if let currentUser = user {
                    // 使用与User.swift中isMemberInfoComplete相同的逻辑
                    let nameEmpty = currentUser.name?.isEmpty ?? true
                    let phoneEmpty = currentUser.phoneNumber.isEmpty
                    let emailEmpty = currentUser.email?.isEmpty ?? true
                    let birthInfoIncomplete = currentUser.birthYear?.isEmpty ?? true || currentUser.birthMonth?.isEmpty ?? true
                    
                    if nameEmpty || phoneEmpty || emailEmpty || birthInfoIncomplete {
                        return .memberInfoComplete
                    }
                } else {
                    return .loginRequired
                }
            case "CASH_VOUCHER_1", "CASH_VOUCHER_2", "CASH_VOUCHER_3":
                // 现金券特殊处理：根据配置决定显示策略
                if AppConfig.CashVoucher.codes.contains(couponCode) {
                    switch AppConfig.CashVoucher.displayStrategy {
                    case .alwaysShow:
                        // 旧行为：总是显示，根据是否拥有决定状态
                        if let currentUser = user {
                            let hasValidCoupon = currentUser.ownedCoupons.contains { userCoupon in
                                userCoupon.couponCode == couponCode &&
                                !userCoupon.isUsed &&
                                userCoupon.expiryDate > Date()
                            }
                            if !hasValidCoupon {
                                return .activationConditions(couponData.activationConditions ?? "")
                            }
                        } else {
                            return .loginRequired
                        }
                    case .showAfterPurchase:
                        // 新行为：只有购买后才显示
                        if let currentUser = user {
                            let hasValidCoupon = currentUser.ownedCoupons.contains { userCoupon in
                                userCoupon.couponCode == couponCode &&
                                !userCoupon.isUsed &&
                                userCoupon.expiryDate > Date()
                            }
                            if !hasValidCoupon {
                                switch AppConfig.CashVoucher.unpurchasedBehavior {
                                case .showWithActivationCondition:
                                    return .activationConditions(couponData.activationConditions ?? "")
                                case .hideCompletely:
                                    return .inactive
                                }
                            }
                        } else {
                            return .inactive // 未登录用户不显示现金券
                        }
                    }
                }
            default:
                // 检查其他激活条件
                if let activationConditions = couponData.activationConditions,
                   !activationConditions.isEmpty {
                    return .activationConditions(activationConditions)
                }
            }
            
            // 检查时间段限制
            if !couponData.timeSlots.isEmpty {
                let now = Date()
                let calendar = Calendar.current
                let weekday = calendar.component(.weekday, from: now)
                let hour = calendar.component(.hour, from: now)
                let minute = calendar.component(.minute, from: now)
                let currentMinutes = hour * 60 + minute
                
                let isInTimeSlot = couponData.timeSlots.contains { timeSlot in
                    timeSlot.weekdays.contains(weekday) &&
                    currentMinutes >= timeSlot.startTime &&
                    currentMinutes <= timeSlot.endTime
                }
                
                if !isInTimeSlot {
                    return .timeSlotRestricted
                }
            }
            
            // 检查使用次数限制
            if let usageLimit = couponData.usageLimit {
                let usageCount = getUserCouponUsageCount(couponCode: couponCode, user: user, usageFrequency: couponData.usageFrequency)
                if usageCount >= usageLimit {
                    return .usageLimitReached
                }
            }
            
            // 检查使用频率限制
            if let currentUser = user {
                // 首先检查用户拥有的礼券是否已被标记为已使用
                let hasUsedCoupon = currentUser.ownedCoupons.contains { userCoupon in
                    userCoupon.couponCode == couponCode && userCoupon.isUsed
                }
                
                if hasUsedCoupon && couponData.usageFrequency == .once {
                    return .usageFrequencyReached
                }
                
                let usageCount = getUserCouponUsageCount(couponCode: couponCode, user: currentUser, usageFrequency: couponData.usageFrequency)
                
                // 对于有使用频率限制的优惠券，检查当前周期内是否已使用
                if usageCount > 0 {
                    switch couponData.usageFrequency {
                    case .once:
                        // 终身只能使用一次，已使用则不可用
                        return .usageFrequencyReached
                    case .daily, .weekly, .monthly, .yearly:
                        // 周期性使用限制，当前周期内已使用则不可用
                        return .usageFrequencyReached
                    case .perOrder:
                        // 每单限用一次，在购物车中处理限制逻辑，这里不限制
                        break
                    }
                }
            }
            
            return .none
        }
        
        /// 获取用户优惠券使用次数
        private static func getUserCouponUsageCount(couponCode: String, user: User?, usageFrequency: UsageFrequency) -> Int {
            guard let user = user else { return 0 }
            
            let calendar = Calendar.current
            let now = Date()
            
            switch usageFrequency {
            case .once:
                // 终身只能使用一次
                return user.couponUsageHistory.filter { $0.couponCode == couponCode }.count
                
            case .daily:
                // 每日限用一次
                let startOfDay = calendar.startOfDay(for: now)
                return user.couponUsageHistory.filter { record in
                    record.couponCode == couponCode &&
                    record.usedDate >= startOfDay
                }.count
                
            case .weekly:
                // 每周限用一次
                guard let currentWeekInterval = calendar.dateInterval(of: .weekOfYear, for: now) else {
                    return 0
                }
                return user.couponUsageHistory.filter { record in
                    record.couponCode == couponCode &&
                    currentWeekInterval.contains(record.usedDate)
                }.count
                
            case .monthly:
                // 每月限用一次
                let startOfMonth = calendar.dateInterval(of: .month, for: now)?.start ?? now
                return user.couponUsageHistory.filter { record in
                    record.couponCode == couponCode &&
                    record.usedDate >= startOfMonth
                }.count
                
            case .yearly:
                // 每年限用一次
                let startOfYear = calendar.dateInterval(of: .year, for: now)?.start ?? now
                return user.couponUsageHistory.filter { record in
                    record.couponCode == couponCode &&
                    record.usedDate >= startOfYear
                }.count
                
            case .perOrder:
                // 每单限用一次，在购物车中处理限制逻辑
                return 0
            }
        }
        
        /// 获取解锁条件的本地化消息
        static func getUnlockMessage(for conditionType: UnlockConditionType, language: String) -> String {
            switch conditionType {
            case .loginRequired:
                return language == "zh" ? AppStrings.Coupons.UnlockMessages.loginRequired : AppStrings.Coupons.UnlockMessages.loginRequiredEn
            case .membershipLevel(let level):
                let baseMessage = language == "zh" ? AppStrings.Coupons.UnlockMessages.membershipLevelRequired : AppStrings.Coupons.UnlockMessages.membershipLevelRequiredEn
                return "\(baseMessage): \(level)"
            case .memberInfoComplete:
                return language == "zh" ? AppStrings.Coupons.UnlockMessages.memberInfoRequired : AppStrings.Coupons.UnlockMessages.memberInfoRequiredEn
            case .birthdayMonthRequired:
                return language == "zh" ? "仅限生日月份使用" : "Available only during birthday month"
            case .activationConditions(let conditions):
                return conditions
            case .timeSlotRestricted:
                return language == "zh" ? AppStrings.Coupons.UnlockMessages.timeSlotRestricted : AppStrings.Coupons.UnlockMessages.timeSlotRestrictedEn
            case .usageLimitReached:
                return language == "zh" ? AppStrings.Coupons.UnlockMessages.usageLimitReached : AppStrings.Coupons.UnlockMessages.usageLimitReachedEn
            case .usageFrequencyReached:
                return language == "zh" ? "本周期内已使用" : "Already used in current period"
            case .expired:
                return language == "zh" ? AppStrings.Coupons.UnlockMessages.couponExpired : AppStrings.Coupons.UnlockMessages.couponExpiredEn
            case .inactive:
                return language == "zh" ? AppStrings.Coupons.UnlockMessages.couponInactive : AppStrings.Coupons.UnlockMessages.couponInactiveEn
            case .none:
                return ""
            }
        }
    }
    
    // MARK: - 优惠券配置
    static let coupons: [CouponData] = [
        // 1. 新会员立减福利
        CouponData(
            code: "NEWMEMBER5",
            title: "新会员立减福利",
            englishTitle: "New Member Discount",
            description: "立减 S$5",
            englishDescription: "S$5 off first order",
            discountType: Coupon.DiscountType.fixedAmount,
            discountValue: 5,
            minimumSpend: 20,
            maximumDiscount: nil as Double?,
            validDays: 30,
            isActive: true,
            usageLimit: 1,
            excludeAlcoholicBeverages: true,
            timeSlots: [
                TimeSlotData(weekdays: [2, 3, 4, 5, 6], startTime: 660, endTime: 1140), // 周一至周五 11:00-19:00
                TimeSlotData(weekdays: [1, 7], startTime: 660, endTime: 1200) // 周六周日 11:00-20:00
            ],
            usageFrequency: UsageFrequency.once,
            relativeDuration: RelativeDuration.days(30),
            reminderDays: 3,
            activationConditions: "注册会员",
            englishActivationConditions: "Register as member",
            requiresLogin: true,
            requiresMembershipLevel: nil as String?
        ),
        
        // 2. 燃动打卡券
        CouponData(
            code: "FUELUP_STREAK",
            title: "燃动打卡券",
            englishTitle: "Fuel-Up Streak Pass",
            description: "立减 S$7",
            englishDescription: "S$7 off sitewide",
            discountType: Coupon.DiscountType.fixedAmount,
            discountValue: 7,
            minimumSpend: 20,
            maximumDiscount: nil as Double?,
            validDays: 30,
            isActive: true,
            usageLimit: nil as Int?,
            excludeAlcoholicBeverages: true,
            timeSlots: [
                TimeSlotData(weekdays: [2, 3, 4, 5, 6], startTime: 660, endTime: 1140), // 周一至周五 11:00-19:00
                TimeSlotData(weekdays: [1, 7], startTime: 660, endTime: 1200) // 周六周日 11:00-20:00
            ],
            usageFrequency: UsageFrequency.daily,
            relativeDuration: RelativeDuration.months(1),
            reminderDays: 3,
            activationConditions: "每周到店消费 2 次以上，每次消费金额需满 S$10，连续 4 周不间断完成，礼券将在第四周结束后自动发放",
            englishActivationConditions: "Visit at least 2 times per week, spend a minimum of S$10 per visit, complete the streak for 4 consecutive weeks, voucher will be issued automatically after the 4th week",
            requiresLogin: true,
            requiresMembershipLevel: nil as String?
        ),
        
        // 3. 会员周末专享
        CouponData(
            code: "WEEKEND50",
            title: "会员周末专享",
            englishTitle: "Weekend Exclusive for Members",
            description: "50%",
            englishDescription: "50% off storewide",
            discountType: Coupon.DiscountType.percentage,
            discountValue: 50,
            minimumSpend: 20,
            maximumDiscount: nil,
            validDays: 18250, // 50年
            isActive: true,
            usageLimit: nil,
            excludeAlcoholicBeverages: true,
            timeSlots: [
                TimeSlotData(weekdays: [1, 7], startTime: 660, endTime: 1200) // 周六周日 11:00-20:00
            ],
            usageFrequency: UsageFrequency.daily,
            relativeDuration: RelativeDuration.years(50),
            reminderDays: 3,
            activationConditions: "注册成为会员，周末时段自动解锁",
            englishActivationConditions: "Register as a member to auto-unlock during weekend hours",
            requiresLogin: true,
            requiresMembershipLevel: nil
        ),
        
        // 4. 会员 HighBall专享
        CouponData(
            code: "HIGHBALL",
            title: "会员 HighBall专享",
            englishTitle: "HighBall Member Exclusive",
            description: "免费",
            englishDescription: "Free HighBall or Whiskey (U.P. S$10/glass)",
            discountType: Coupon.DiscountType.fixedAmount,
            discountValue: 10,
            minimumSpend: 20,
            maximumDiscount: nil,
            validDays: 18250, // 50年
            isActive: true,
            usageLimit: 1,
            excludeAlcoholicBeverages: false,
            timeSlots: [
                TimeSlotData(weekdays: [2, 3, 4, 5, 6], startTime: 660, endTime: 1140), // 周一至周五 11:00-19:00
                TimeSlotData(weekdays: [1, 7], startTime: 660, endTime: 1200) // 周六周日 11:00-20:00
            ],
            usageFrequency: UsageFrequency.weekly,
            relativeDuration: RelativeDuration.years(50),
            reminderDays: 3,
            activationConditions: "注册成为会员",
            englishActivationConditions: "Register as a member",
            requiresLogin: true,
            requiresMembershipLevel: nil
        ),
        
        // 5. 小牛欢迎咖啡
        CouponData(
            code: "CALFCOFFEE",
            title: "小牛欢迎咖啡",
            englishTitle: "Calf Welcome Coffee",
            description: "免费",
            englishDescription: "Free coffee (one cup per week)",
            discountType: .fixedAmount,
            discountValue: 2,
            minimumSpend: 10,
            maximumDiscount: nil,
            validDays: 18250, // 50年
            isActive: true,
            usageLimit: 1,
            excludeAlcoholicBeverages: true,
            timeSlots: [
                TimeSlotData(weekdays: [2, 3, 4, 5, 6], startTime: 660, endTime: 1140), // 周一至周五 11:00-19:00
                TimeSlotData(weekdays: [1, 7], startTime: 660, endTime: 1200) // 周六周日 11:00-20:00
            ],
            usageFrequency: UsageFrequency.weekly,
            relativeDuration: RelativeDuration.years(50),
            reminderDays: 3,
            activationConditions: "注册成为会员",
            englishActivationConditions: "Register as a member",
            requiresLogin: true,
            requiresMembershipLevel: nil
        ),
        
        // 6. 小牛「神秘礼品」
        CouponData(
            code: "MYSTERYGIFT",
            title: "小牛「神秘礼品」",
            englishTitle: "Calf 'Mystery Gift'",
            description: "免费",
            englishDescription: "Free",
            discountType: .fixedAmount,
            discountValue: 0,
            minimumSpend: 10,
            maximumDiscount: nil,
            validDays: 18250, // 50年
            isActive: true,
            usageLimit: 1,
            excludeAlcoholicBeverages: false,
            timeSlots: [
                TimeSlotData(weekdays: [2, 3, 4, 5, 6], startTime: 660, endTime: 1140), // 周一至周五 11:00-19:00
                TimeSlotData(weekdays: [1, 7], startTime: 660, endTime: 1200) // 周六周日 11:00-20:00
            ],
            usageFrequency: UsageFrequency.once,
            relativeDuration: RelativeDuration.years(50),
            reminderDays: 3,
            activationConditions: "注册成为会员",
            englishActivationConditions: "Register as a member",
            requiresLogin: true,
            requiresMembershipLevel: nil
        ),
        
        // 7. 紫域主理人专享
        CouponData(
            code: "VIBE_REGENT",
            title: "紫域主理人专享",
            englishTitle: "Vibe Regent Benefit",
            description: "立减 S$5",
            englishDescription: "S$5 off sitewide",
            discountType: .fixedAmount,
            discountValue: 5,
            minimumSpend: 20,
            maximumDiscount: nil,
            validDays: 18250, // 50年
            isActive: true,
            usageLimit: nil,
            excludeAlcoholicBeverages: true,
            timeSlots: [
                TimeSlotData(weekdays: [2, 3, 4, 5, 6], startTime: 660, endTime: 1140), // 周一至周五 11:00-19:00
                TimeSlotData(weekdays: [1, 7], startTime: 660, endTime: 1200) // 周六周日 11:00-20:00
            ],
            usageFrequency: UsageFrequency.monthly,
            relativeDuration: RelativeDuration.years(50),
            reminderDays: 3,
            activationConditions: "购买现金礼券配套",
            englishActivationConditions: "After membership level up to Vibe Regent",
            requiresLogin: true,
            requiresMembershipLevel: "Vibe Regent"
        ),
        
        // 8. 生日月份专属福利
        CouponData(
            code: "BIRTHDAY30",
            title: "生日月份专属福利",
            englishTitle: "Birthday Month Reward",
            description: "全场 30% 折扣",
            englishDescription: "30% off sitewide",
            discountType: Coupon.DiscountType.percentage,
            discountValue: 30,
            minimumSpend: 30,
            maximumDiscount: nil,
            validDays: 18250, // 50年
            isActive: true,
            usageLimit: 1,
            excludeAlcoholicBeverages: true,
            timeSlots: [
                TimeSlotData(weekdays: [2, 3, 4, 5, 6], startTime: 660, endTime: 1140), // 周一至周五 11:00-19:00
                TimeSlotData(weekdays: [1, 7], startTime: 660, endTime: 1200) // 周六周日 11:00-20:00
            ],
            usageFrequency: UsageFrequency.yearly,
            relativeDuration: RelativeDuration.years(50),
            reminderDays: 3,
            activationConditions: "注册会员并完善个人资料",
            englishActivationConditions: "Register and complete profile",
            requiresLogin: true,
            requiresMembershipLevel: nil
        ),
        
        // 9. 橙燃玩家专享
        CouponData(
            code: "HYPE_BLAZE",
            title: "橙燃玩家专享",
            englishTitle: "Hype Blaze Benefit",
            description: "全场 10% 折扣",
            englishDescription: "10% off sitewide",
            discountType: Coupon.DiscountType.percentage,
            discountValue: 10,
            minimumSpend: 20,
            maximumDiscount: nil,
            validDays: 18250, // 50年
            isActive: true,
            usageLimit: nil,
            excludeAlcoholicBeverages: true,
            timeSlots: [
                TimeSlotData(weekdays: [2, 3, 4, 5, 6], startTime: 660, endTime: 1140), // 周一至周五 11:00-19:00
                TimeSlotData(weekdays: [1, 7], startTime: 660, endTime: 1200) // 周六周日 11:00-20:00
            ],
            usageFrequency: UsageFrequency.weekly,
            relativeDuration: RelativeDuration.years(50),
            reminderDays: 3,
            activationConditions: "会员级别升级为橙燃玩家",
            englishActivationConditions: "Membership upgraded to Orange Blaze",
            requiresLogin: true,
            requiresMembershipLevel: "Hype Blaze"
        ),
        
        // 10. 会员信息完善礼券
        CouponData(
            code: "MEMBER_INFO_COMPLETE",
            title: "资料完善专属福利",
            englishTitle: "Profile Completion Reward",
            description: "立减 S$3",
            englishDescription: "S$3 off sitewide",
            discountType: .fixedAmount,
            discountValue: 3,
            minimumSpend: 20,
            maximumDiscount: nil,
            validDays: 30,
            isActive: true,
            usageLimit: 1,
            excludeAlcoholicBeverages: true,
            timeSlots: [
                TimeSlotData(weekdays: [2, 3, 4, 5, 6], startTime: 660, endTime: 1140), // 周一至周五 11:00-19:00
                TimeSlotData(weekdays: [1, 7], startTime: 660, endTime: 1200) // 周六周日 11:00-20:00
            ],
            usageFrequency: UsageFrequency.once,
            relativeDuration: RelativeDuration.days(30),
            reminderDays: 3,
            activationConditions: "注册会员并完善个人资料",
            englishActivationConditions: "Register and complete profile",
            requiresLogin: true,
            requiresMembershipLevel: nil
        ),
        
        // 11. 现金券 1
        CouponData(
            code: "CASH_VOUCHER_1",
            title: "现金券 1",
            englishTitle: "Cash Voucher 1",
            description: "全场立减 S$\(Int(AppConfig.CashVoucherPackage.voucherValues[0]))",
            englishDescription: "S$\(Int(AppConfig.CashVoucherPackage.voucherValues[0])) off sitewide",
            discountType: Coupon.DiscountType.fixedAmount,
            discountValue: AppConfig.CashVoucherPackage.voucherValues[0],
            minimumSpend: AppConfig.CashVoucherPackage.minimumSpends[0],
            maximumDiscount: nil as Double?,
            validDays: AppConfig.CashVoucherPackage.voucherValidityYears * 365,
            isActive: true,
            usageLimit: 1,
            excludeAlcoholicBeverages: true,
            timeSlots: [
                TimeSlotData(weekdays: [2, 3, 4, 5, 6], startTime: 660, endTime: 1140), // 周一至周五 11:00-19:00
                TimeSlotData(weekdays: [1, 7], startTime: 660, endTime: 1200) // 周六周日 11:00-20:00
            ],
            usageFrequency: UsageFrequency.perOrder,
            relativeDuration: RelativeDuration.days(AppConfig.CashVoucherPackage.voucherValidityYears * 365),
            reminderDays: 7,
            activationConditions: "购买现金礼券配套",
            englishActivationConditions: "Purchase Cash Voucher Package",
            requiresLogin: true,
            requiresMembershipLevel: nil as String?
        ),
        
        // 12. 现金券 2
        CouponData(
            code: "CASH_VOUCHER_2",
            title: "现金券 2",
            englishTitle: "Cash Voucher 2",
            description: "全场立减 S$\(Int(AppConfig.CashVoucherPackage.voucherValues[1]))",
            englishDescription: "S$\(Int(AppConfig.CashVoucherPackage.voucherValues[1])) off sitewide",
            discountType: Coupon.DiscountType.fixedAmount,
            discountValue: AppConfig.CashVoucherPackage.voucherValues[1],
            minimumSpend: AppConfig.CashVoucherPackage.minimumSpends[1],
            maximumDiscount: nil as Double?,
            validDays: AppConfig.CashVoucherPackage.voucherValidityYears * 365,
            isActive: true,
            usageLimit: 1,
            excludeAlcoholicBeverages: true,
            timeSlots: [
                TimeSlotData(weekdays: [2, 3, 4, 5, 6], startTime: 660, endTime: 1140), // 周一至周五 11:00-19:00
                TimeSlotData(weekdays: [1, 7], startTime: 660, endTime: 1200) // 周六周日 11:00-20:00
            ],
            usageFrequency: UsageFrequency.perOrder,
            relativeDuration: RelativeDuration.days(AppConfig.CashVoucherPackage.voucherValidityYears * 365),
            reminderDays: 7,
            activationConditions: "购买现金礼券配套",
            englishActivationConditions: "Purchase Cash Voucher Package",
            requiresLogin: true,
            requiresMembershipLevel: nil as String?
        ),
        
        // 现金券 3
        CouponData(
            code: "CASH_VOUCHER_3",
            title: "现金券 3",
            englishTitle: "Cash Voucher 3",
            description: "全场立减 S$\(Int(AppConfig.CashVoucherPackage.voucherValues[2]))",
            englishDescription: "S$\(Int(AppConfig.CashVoucherPackage.voucherValues[2])) off sitewide",
            discountType: Coupon.DiscountType.fixedAmount,
            discountValue: AppConfig.CashVoucherPackage.voucherValues[2],
            minimumSpend: AppConfig.CashVoucherPackage.minimumSpends[2],
            maximumDiscount: nil as Double?,
            validDays: AppConfig.CashVoucherPackage.voucherValidityYears * 365,
            isActive: true,
            usageLimit: 1,
            excludeAlcoholicBeverages: true,
            timeSlots: [
                TimeSlotData(weekdays: [2, 3, 4, 5, 6], startTime: 660, endTime: 1140), // 周一至周五 11:00-19:00
                TimeSlotData(weekdays: [1, 7], startTime: 660, endTime: 1200) // 周六周日 11:00-20:00
            ],
            usageFrequency: UsageFrequency.perOrder,
            relativeDuration: RelativeDuration.days(AppConfig.CashVoucherPackage.voucherValidityYears * 365),
            reminderDays: 7,
            activationConditions: "购买现金礼券配套",
            englishActivationConditions: "Purchase Cash Voucher Package",
            requiresLogin: true,
            requiresMembershipLevel: nil as String?
        ),
        
        // 3-Day Hustle 礼券
        CouponData(
            code: "THREE_DAY_HUSTLE",
            title: "3-Day Hustle",
            englishTitle: "3-Day Hustle",
            description: "立减 S$5",
            englishDescription: "S$5 off",
            discountType: Coupon.DiscountType.fixedAmount,
            discountValue: 5,
            minimumSpend: 10,
            maximumDiscount: nil as Double?,
            validDays: 365, // 一年有效期
            isActive: true,
            usageLimit: 1,
            excludeAlcoholicBeverages: true,
            timeSlots: [
                TimeSlotData(weekdays: [2, 3, 4, 5, 6], startTime: 660, endTime: 1140), // 周一至周五 11:00-19:00
                TimeSlotData(weekdays: [1, 7], startTime: 660, endTime: 1200) // 周六周日 11:00-20:00
            ],
            usageFrequency: UsageFrequency.once,
            relativeDuration: RelativeDuration.days(365),
            reminderDays: 3,
            activationConditions: "一周内任意三天消费，每次消费金额需满 S$10",
            englishActivationConditions: "Make purchases on any three days within a week, with each transaction at least S$10",
            requiresLogin: true,
            requiresMembershipLevel: nil as String?
        )
    ]
}

// MARK: - 优惠券数据结构
struct CouponData {
    let code: String
    let title: String
    let englishTitle: String
    let description: String
    let englishDescription: String
    let discountType: Coupon.DiscountType
    let discountValue: Double
    let minimumSpend: Double
    let maximumDiscount: Double?
    let validDays: Int
    let isActive: Bool
    let usageLimit: Int?
    let excludeAlcoholicBeverages: Bool
    let timeSlots: [TimeSlotData]
    let usageFrequency: UsageFrequency
    let relativeDuration: RelativeDuration
    let reminderDays: Int
    let activationConditions: String?
    let englishActivationConditions: String?
    let requiresLogin: Bool
    let requiresMembershipLevel: String?
}

// MARK: - 时间段数据结构
struct TimeSlotData {
    let weekdays: [Int]
    let startTime: Int
    let endTime: Int
}

// MARK: - 扩展方法
extension CouponData {
    func toCoupon() -> Coupon {
        return Coupon(
            code: code,
            title: title,
            englishTitle: englishTitle,
            description: description,
            englishDescription: englishDescription,
            discountType: discountType,
            discountValue: discountValue,
            minimumSpend: minimumSpend,
            maximumDiscount: maximumDiscount,
            validFrom: Date(),
            validUntil: Calendar.current.date(byAdding: .day, value: validDays, to: Date()) ?? Date(),
            isActive: isActive,
            usageLimit: usageLimit,
            usageCount: 0,
            excludeAlcoholicBeverages: excludeAlcoholicBeverages,
            applicableTimeSlots: timeSlots.map { $0.toTimeSlot() },
            usageFrequency: usageFrequency,
            relativeDuration: relativeDuration,
            reminderDays: reminderDays,
            activationConditions: activationConditions,
            englishActivationConditions: englishActivationConditions,
            requiresLogin: requiresLogin,
            requiresMembershipLevel: requiresMembershipLevel
        )
    }
}

extension TimeSlotData {
    func toTimeSlot() -> TimeSlot {
        return TimeSlot(
            weekdays: weekdays,
            startTime: startTime,
            endTime: endTime
        )
    }
}